exports.ElevatorHandler = Core.Class.singleton(
{
    classname: 'ElevatorHandler',
    
    initialize: function() 
    {
    	this._sourceFloorArray = null;
    	this._elevatorsUIArray = null;
    	
    	this._elevatorsUIArray = new GAMutableArray();
    	this._interestedAvatarsArray = new GAMutableArray();
    },
    
    initializeAllElevators: function()
    {
    	var allElevatorsModels = MMGameLayer.getMallUI().getMallObj()._allElevators;
    	for(var i=0 ; i<allElevatorsModels.getLength() ; i++)
    	{
    		var pickSide = 0;
    		if(i%2 == 0)
    		{
    			pickSide = kSide.kSideLeft;
    		} 
    		else
    		{
    			pickSide = kSide.kSideRight;
    		}
	    	var model = allElevatorsModels.objectAtIndex(i);
			var elevatorUI = new ElevatorUI(model, pickSide);
			elevatorUI.createView();
			elevatorUI.setDepth(99999);
			MMGameLayer.getMallUI().addChild(elevatorUI);
			
			this._elevatorsUIArray.addObject(elevatorUI);
    	}
    },
    
    addElevator: function(itemId)
    {
		if(itemId && this._elevatorsUIArray)
		{		
			var elevatorModel = MMGameLayer.getMallUI().getMallObj().addElevator(itemId);
			
			var pickSide = kSide.kSideRight;
			if(this._elevatorsUIArray.getLength() % 2 == 0)
			{
				pickSide = kSide.kSideLeft;
			}
			var elevatorUI = new ElevatorUI(elevatorModel, pickSide);
			elevatorUI.createView();
			elevatorUI.setDepth(99999);
			MMGameLayer.getMallUI().addChild(elevatorUI);
			//MMGameLayer.getControlLayer().addChild(elevatorUI);
			
			this._elevatorsUIArray.addObject(elevatorUI);
		}
    },
    
    addMoreElevatorsFloor: function()
    {
    	//if(this._elevatorsUIArray)
    	{
	    	for(var i=0 ; i<this._elevatorsUIArray.getLength() ; i++)
	    	{
	    		var elevatorUI = this._elevatorsUIArray.objectAtIndex(i);
	    		elevatorUI.addDestinationFloorRow();
	    	}
    	}
    },
    
    addInterestedAvatar: function(avatarUI)
    {
    	
    	if(this._interestedAvatarsArray)
    	{
    		this._interestedAvatarsArray.addObject(avatarUI);
    	}
    	this.checkAndAssignElevator();
    },
    
    checkAndAssignElevator: function()
    {
    	for(var i=0 ; i<this._interestedAvatarsArray.getLength() ; i++)
    	{
    		var interestedAvatarUI = this._interestedAvatarsArray.objectAtIndex(i);
    		var startIndex = 0;
    		if(interestedAvatarUI.getPickSide() == kSide.kSideRight)
    		{
    			startIndex = 1;
    		}
    		for(var j=startIndex ; j<this._elevatorsUIArray.getLength() ; j+=2)
    		{
    			var elevatorUI = this._elevatorsUIArray.objectAtIndex(j); 
    			if(elevatorUI.canAvatarBeAddedForDestination(interestedAvatarUI.getAvatarSourceFloor()))
    			{
    				elevatorUI.addDestinationFloor(interestedAvatarUI.getAvatarSourceFloor());
    				break;
    			}
    		}
    	}
    },
    
    checkInsAndOutsForElevator: function(elevatorUI)
    {
    	this.checkOutsForElevator(elevatorUI);
    	this.checkInsForElevator(elevatorUI);
    },
    
    checkInsForElevator: function(elevatorUI)
    {
    	//Checking Ins
    	for(var i=0 ; i<this._interestedAvatarsArray.getLength() ;)
    	{
    		var interestedAvatarUI = this._interestedAvatarsArray.objectAtIndex(i);
    		if(interestedAvatarUI.getAvatarSourceFloor() == elevatorUI.getCurrentFloor() && !elevatorUI.isElevatorFull() && !elevatorUI.getIsMoving() && elevatorUI.getPickSide() == interestedAvatarUI.getPickSide())
    		{
    			interestedAvatarUI.removeAvatarFromFloor();
    			elevatorUI.addAvatarToElevator(interestedAvatarUI);
    			elevatorUI.addDestinationFloor(interestedAvatarUI.getDestinationFloor());
    			this._interestedAvatarsArray.removeObjectAtIndex(i);
    			//elevatorUI.extractFloorAndMove();
    		} 
    		else
    		{
    			i++;
    		}
    	}
    },
    
    checkOutsForElevator: function(elevatorUI)
    {
    	//Checking Outs
    	var avatarsUIArray = elevatorUI.getAvatarsUIArray();
    	for(var i=0 ; i<avatarsUIArray.getLength() ;)
    	{
    		var avatarUI = avatarsUIArray.objectAtIndex(i);
    		if(avatarUI && avatarUI.getDestinationFloor() == elevatorUI.getCurrentFloor())
    		{
    			//remove avatarUI Object from array
    			console.log("Zulu: checkInsAndOutsForElevator OUTSSSSSS CASE "+avatarUI.getDestinationFloor());
    			avatarUI.unloadAvatarAtFloor(avatarUI.getDestinationFloor());
    			avatarsUIArray.removeObjectAtIndex(i);
    		}
    		else
    		{
    			i++;
    		}
    	}
    },
    
    destroyElevatorUIObjects: function()
    {
    	var count = this._elevatorsUIArray.getLength();
    	for(var i = 0; i < count;)
    	{
    		this._elevatorsUIArray.objectAtIndex(i).destroy();
    		this._elevatorsUIArray.removeObjectAtIndex(i);
    		count--;
    	}
    },
    
    getElevatorsUIArray: function()
    {
    	return this._elevatorsUIArray;
    },
	
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/environmentlayer/storeui/";
	},
								   
	destroy: function()
    {
    	
    },
});
